package org.dromara.demo.controller;

import java.util.List;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.demo.domain.vo.JsCaseVarVo;
import org.dromara.demo.domain.vo.JsExampleVo;
import org.dromara.demo.service.IJsCaseVarService;
import org.dromara.demo.service.IJsExampleService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.demo.domain.vo.JsSuiteBasicVarVo;
import org.dromara.demo.domain.bo.JsSuiteBasicVarBo;
import org.dromara.demo.service.IJsSuiteBasicVarService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 套件参数
 *
 * @author zhu
 * @date 2025-02-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/suiteBasicVar")
public class JsSuiteBasicVarController extends BaseController {

    private final IJsSuiteBasicVarService jsSuiteBasicVarService;

    private final IJsExampleService jsExampleService;

    private final IJsCaseVarService jsCaseVarService;
    /**
     * 查询套件参数列表
     */
    @GetMapping("/list")
    public TableDataInfo<JsSuiteBasicVarVo> list(JsSuiteBasicVarBo bo, PageQuery pageQuery) {
        return jsSuiteBasicVarService.queryPageList(bo, pageQuery);
    }

    /**
     * 根据id获取套件参数
     */
    @GetMapping("/getSetting/{suiteId}")
    public R<JsSuiteBasicVarVo> getById(@NotNull(message = "主键不能为空")
                                     @PathVariable Long suiteId) {
        JsSuiteBasicVarVo jsSuiteBasicVarVo = jsSuiteBasicVarService.queryBySuiteId(suiteId);
        if (jsSuiteBasicVarVo == null) {
           throw new ServiceException("未查询到套件参数");
        }
        String exampleIds = jsSuiteBasicVarVo.getExampleIds();
        String[] split = exampleIds.split(StringUtils.SEPARATOR);
        List<JsExampleVo> jsExampleVos = jsExampleService.queryByIds(List.of(split));
        List<String> collect = jsExampleVos.stream().map(JsExampleVo::getElementId).collect(Collectors.toList());
        List<JsCaseVarVo> jsCaseVarVos = jsCaseVarService.queryByIds(collect);
        List<JsExampleVo> jsExampleVos1 = jsExampleVos.stream().peek(jsExampleVo -> jsExampleVo.setJsCaseVarVos(
            jsCaseVarVos.stream().filter(jsCaseVarVo -> jsExampleVo.getElementId()
                .equals(jsCaseVarVo.getExampleLocation())).collect(Collectors.toList()))).collect(Collectors.toList());

        jsSuiteBasicVarVo.setJsExampleVos(jsExampleVos1);
        return R.ok(jsSuiteBasicVarVo);
    }

    /**
     * 导出套件参数列表
     */
    @Log(title = "套件参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(JsSuiteBasicVarBo bo, HttpServletResponse response) {
        List<JsSuiteBasicVarVo> list = jsSuiteBasicVarService.queryList(bo);
        ExcelUtil.exportExcel(list, "套件参数", JsSuiteBasicVarVo.class, response);
    }

    /**
     * 获取套件参数详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<JsSuiteBasicVarVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(jsSuiteBasicVarService.queryById(id));
    }

    /**
     * 新增套件参数
     */
    @Log(title = "套件参数", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody JsSuiteBasicVarBo bo) {
        return toAjax(jsSuiteBasicVarService.insertByBo(bo));
    }

    /**
     * 修改套件参数
     */
    @Log(title = "套件参数", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody JsSuiteBasicVarBo bo) {
        return toAjax(jsSuiteBasicVarService.updateByBo(bo));
    }

    /**
     * 删除套件参数
     *
     * @param ids 主键串
     */
    @Log(title = "套件参数", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(jsSuiteBasicVarService.deleteWithValidByIds(List.of(ids), true));
    }
}
